home *** CD-ROM | disk | FTP | other *** search
/ CICA 1993 April / CICA MS Windows - April 1993.iso / unzipped / programr / listings / ptv2n5 / l2.asm < prev    next >
Assembly Source File  |  1991-09-07  |  2KB  |  68 lines

  1. ; Divides an arbitrarily long unsigned dividend by a 16-bit unsigned
  2. ; divisor. C near-callable as:
  3. ;    unsigned int Div(unsigned int * Dividend,
  4. ;        int DividendLength, unsigned int Divisor,
  5. ;        unsigned int * Quotient);
  6. ;
  7. ; Returns the remainder of the division.
  8. ;
  9. ; Tested with TASM 2.
  10.  
  11. parms    struc
  12.     dw    2 dup (?) ;pushed BP & return address
  13. Dividend dw    ?    ;pointer to value to divide, stored in Intel
  14.             ; order, with lsb at lowest address, msb at
  15.             ; highest. Must be composed of an integral
  16.             ; number of words
  17. DividendLength dw ?    ;# of bytes in Dividend. Must be a multiple
  18.             ; of 2
  19. Divisor    dw    ?    ;value by which to divide. Must not be zero,
  20.             ; or a Divide By Zero interrupt will occur
  21. Quotient dw    ?    ;pointer to buffer in which to store the
  22.             ; result of the division, in Intel order.
  23.             ; The quotient returned is of the same
  24.             ; length as the dividend
  25. parms    ends
  26.  
  27.     .model    small
  28.     .code
  29.     public    _Div
  30. _Div    proc    near
  31.         push    bp      ;preserve caller's stack frame
  32.         mov     bp,sp   ;point to our stack frame
  33.         push    si      ;preserve caller's register variables
  34.         push    di
  35.  
  36.     std        ;we're working from msb to lsb
  37.     mov    ax,ds
  38.     mov    es,ax    ;for STOS
  39.     mov    cx,[bp+DividendLength]
  40.     sub    cx,2
  41.     mov    si,[bp+Dividend]
  42.     add    si,cx    ;point to the last word of the dividend
  43.             ; (the most significant word)
  44.     mov    di,[bp+Quotient]
  45.     add    di,cx    ;point to the last word of the quotient
  46.             ; buffer (the most significant word)
  47.     mov    bx,[bp+Divisor]
  48.     shr    cx,1
  49.     inc    cx    ;# of words to process
  50.     sub    dx,dx    ;convert initial divisor word to a 32-bit
  51.             ; value for DIV
  52. DivLoop:
  53.     lodsw        ;get next most significant word of divisor
  54.     div    bx
  55.     stosw        ;save this word of the quotient
  56.             ;DX contains the remainder at this point,
  57.             ; ready to prepend to the next divisor word
  58.     loop    DivLoop
  59.     mov    ax,dx    ;return the remainder
  60.  
  61.     cld        ;restore default Direction flag setting
  62.         pop     di      ;restore caller's register variables
  63.         pop     si
  64.         pop     bp      ;restore caller's stack frame
  65.         ret
  66. _Div    endp
  67.     end
  68.